package com.james.happyfarm.modules;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.james.happyfarm.PO.Farm;
import com.james.happyfarm.PO.Friend;
import com.james.happyfarm.PO.Garden;
import com.james.happyfarm.network.KaiXinWap;

public class GardenModule {
	public final static String[] GUARD = {
		"\u83DC\u8001\u4F2F", //菜老伯
		"\u82B1\u5A46\u5A46" //花婆婆
	};
	
	public final static String[] ADVFRUITS = {
		"\u51AC\u866B\u590F\u8349", //冬虫夏草
		"\u96EA\u83B2", //雪莲
		"\u7075\u829D", //灵芝
		"\u4F55\u9996\u4E4C", //何首乌
		"\u4EBA\u5F62\u4F55\u9996\u4E4C", //人形何首乌
		"\u4EBA\u53C2" //人参
	};
	
	public final static String[] CHEAPFRUITS = {
		"\u7267\u8349", //牧草
		"\u80E1\u841D\u535C", //胡萝卜
		"\u66FC\u9640\u7F57", //曼陀罗
	};

	public List<Friend> getStealableFriend(String verifyCode) {
		List<Friend> friendList = new ArrayList<Friend>();
		friendList.add(new Friend(KaiXinWap.ME_UID, KaiXinWap.ME_USERNAME, ""));
		
		String url = KaiXinWap.KAIXINNETWORK_WAP + "/house/garden/getfriendmature.php?verify=" + verifyCode;
		String resp = KaiXinWap.callURL(url);
		
		String regex = "<a href=\"/house/garden/\\?[^>]*uid=([^;&]*)[^>]*>([^<]*)</a>[^\u5DF2\u6210\u719F]*\u5DF2\u6210\u719F";
		
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(resp);
		while(m.find()){
			friendList.add(new Friend(m.group(1), m.group(2), ""));
		}
		
		return friendList;
	}
	
	public Garden getStealableFarm(Garden garden, String verifyCode) {
		List<Farm> farmList = garden.getFarmList();
		
		String url =  KaiXinWap.KAIXINNETWORK_WAP + "/house/garden/?verify=" + verifyCode + "&fuid=" + garden.getOwner().getUid();
		String resp = KaiXinWap.callURL(url);
		
		resp = resp.replaceAll("aixindi.gif", "\u0000"); //好友地
		resp = resp.replaceAll("\u5DF2\u5077\u8FC7", "\u0001"); //已偷过
		resp = resp.replaceAll("\u53EF\u5077", "\u0002");//再过x分可偷
		resp = resp.replaceAll("\u6536\u83B7", "\u0004");//收获
		
		if (garden.getOwner().getUid().equals(KaiXinWap.ME_UID)) {
			//收获
			String regex = "<a[^>]*href=\"/house/garden/havest.php?[^>]*farmnum=(\\d*)[^>]*>";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(resp);
			while(m.find()) {
				farmList.add(new Farm(Integer.parseInt(m.group(1)), garden));
			}
		} else {
			//偷
			
			//check guard
			for (String guardStr : GUARD) {
				if (resp.indexOf(guardStr) != -1)
					return garden;
			}
			
			String regex = "<div class=\"list\">\\s*([^\uFF1A]{1,10})\uFF1A\\s*<font\\s*color=\"#[0-9A-Za-z]*\">\\s*\u5269\u4F59\\s*(\\d*)\\s*</font>[^\u0000\u0001\u0002\u5077]*<a[^>]*href=\"/house/garden/havest.php?[^>]*farmnum=(\\d*)[^>]*>\u5077";
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(resp);
			boolean flag;
			while(m.find()){
				flag = false;
				for (String advFruit : ADVFRUITS) {
					if (advFruit.equals(m.group(1)) && Integer.parseInt(m.group(2)) <= 1){
						flag = true;
						break;
					}
				}
				for (String cheapFruit : CHEAPFRUITS) {
					if (cheapFruit.equals(m.group(1))){
						flag = true;
						break;
					}
				}
				if (flag)
					continue;
				farmList.add(new Farm(Integer.parseInt(m.group(3)), garden));
			}
		}
		return garden;
	}
	
	public String stealFarm(Farm farm, String verifyCode) {		
		String url = KaiXinWap.KAIXINNETWORK_WAP + "/house/garden/havest.php?verify=" + verifyCode + "&fuid=" + farm.getOwner().getOwner().getUid() + "&farmnum=" + farm.getFarmnum();
		String resp = KaiXinWap.callURL(url);
		
		String regex = "<div class=\"tips1\">([^<]*)</div>";
		
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(resp);
		if(m.find()){
			return m.group(1);
		}
		
		return "no response!";		
	}
}
